package com.hn.shoutchinese.voice.service.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;

import com.hn.comm.Tools;
import com.hn.net.Kit;
import com.hn.shoutchinese.voice.service.IVoiceService;

public class VoiceServiceImpl implements IVoiceService {

	@Override
	public byte[] getVoiceBytes(String word) {
		// http://localhost:8888/data/yuannana.mp3
		// http://translate.google.cn/translate_tts?ie=UTF-8&q=%E7%9F%A5%E9%81%93%E4%BA%86&tl=zh-CN&total=1&idx=0&textlen=2
		String url = "http://translate.google.cn/translate_tts?ie=UTF-8&q=";
		try {
			url += java.net.URLEncoder.encode(word, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		url += "&tl=zh-CN&total=1&idx=0&textlen=3&prev=input";

		// System.out.println(url);

		byte[] arr = Kit
				.httpRequestBytes(url,
						null, "GET", "utf-8");
		return arr;
	}

	@Override
	public void writeToFile(byte[] data, String filePath, String filename) {
		Tools.writeFile(data, filePath + File.separator + filename);
	}

	public static void main(String[] args) {
		// String word="知道了";
		// try {
		// word = java.net.URLEncoder.encode(word,"utf-8");
		// } catch (UnsupportedEncodingException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// System.out.println(word);

		VoiceServiceImpl vsi = new VoiceServiceImpl();
		String chinese = "自己";
		byte[] data = vsi.getVoiceBytes(chinese);
		vsi.writeToFile(data, "D:", chinese + ".mp3");
	}

}
